Source Code Viewer - Example 1 - SearchResults

package name.matthewgreet.example1.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * <P>Record list with index of selected record</P>
 * 
 * <DL>
 *   <DT>T</DT><DD>Record type.</DD>
 * </DL>
 */
public class SearchResults<T> implements Cloneable,Serializable {
    private static final long serialVersionUID = 8649789440628664573L;
    
    private List<T> list;
    private int pageSize;
    private int selectedIndex;	// Index of currently selected record.
    
    
    public SearchResults(SearchResults<T> oldValue) {
    	this.list = new ArrayList<T>(oldValue.getList());
        this.pageSize = oldValue.getPageSize();
        this.selectedIndex = oldValue.getSelectedIndex();
    }
    
    public SearchResults(int pageSize) {
    	this.list = new ArrayList<T>();
        this.pageSize = pageSize;
        this.selectedIndex = -1;
    }
    
    /**
     * Ensures selectedIndex is within list boundary or is -1.
     */
    protected void checkSelectedIndex() {
        int size;
        
        size = list.size();
        if (selectedIndex >= size) {
            selectedIndex = size - 1;
        } else if (selectedIndex < 0) {
            selectedIndex = -1;
        }
    }

    public Object clone() throws CloneNotSupportedException {
    	return super.clone();
    }
    
    public List<T> getList() {
        return list;
    }
    
    public int getListSize() {
        return list.size();
    }
    
    public List<T> getPage() {
        int pageNo, startIndex, endIndex;
        
        if (list.size() > 0) {
            pageNo = getPageNo();
            pageNo = (pageNo < 1)?1:pageNo;
            startIndex =  (pageNo - 1) * pageSize;
            endIndex = startIndex + pageSize - 1;
            endIndex = (endIndex >= list.size())?list.size() - 1:endIndex;
            return list.subList(startIndex, endIndex + 1);
        } else {
            return new ArrayList<T>();
        }
    }
    
    /**
     * Calculates number of page that contains indexed item. 
     */
    public int getPageNo() {
    	if (selectedIndex > -1) {
    		return (selectedIndex / pageSize) + 1;
    	} else {
    		return 0;
    	}
    }
    
	public int getPageSize() {
		return pageSize;
	}
	
	/**
     * Returns selected item from list, which will include any details.
     * @throws IllegalStateException List finder not set and reload required.
     */
    public T getSelected() throws Exception {
        if (selectedIndex >= 0) {
            return list.get(selectedIndex);
        } else {
            return null;
        }
    }

    /**
     * Returns index of currently selected item in list, or -1 if none selected.
     */
    public int getSelectedIndex() {
        return selectedIndex;
    }
    
    public int getTotalPages() {
		return list.size()/pageSize + (list.size()%pageSize == 0?0:1);
	}
    
    public void setList(List<T> list) {
		this.list = list;
    	checkSelectedIndex();
	}
    
    public void setPageNo(int pageNo) {
        selectedIndex = (pageNo - 1) * pageSize;
    	checkSelectedIndex();
    }

    /**
     * Replaces selected item in list. 
     */
    public void setSelected(T value) {
        if (selectedIndex != -1) {
            list.set(selectedIndex, value);
        }
    }

	/**
     * Sets currently selected item in list by index.  Sets to last item if index exceeds last item.  Sets no currently 
     * selected item if index is -1.
     */
    public void setSelectedIndex(int index) {
        selectedIndex = index;
    	checkSelectedIndex();
    }

	public int getPageStartIndex() {
    	if (selectedIndex > -1) {
    		return (selectedIndex / pageSize) * pageSize;
    	} else {
    		return -1;
    	}
	}
}